package CI.aufgabe2.cip.mynodes;

import CI.aufgabe2.cip.SymbolTable;
import CI.aufgabe2.cip.base.AbstractDescr;
import CI.aufgabe2.cip.descriptor.IntConstDescr;
import CI.aufgabe2.cip.descriptor.SimpleTypeDescr;

public class MyTypeDeclarationNode extends MyAbstractNode {

	private static final long serialVersionUID = 1L;

	private final MyIdNode ident;
	private final MyAbstractNode type;

	public MyTypeDeclarationNode(MyIdNode ident, MyAbstractNode type) {
		this.ident = ident;
		this.type = type;
	}

	@Override
	public void print() {
		trace("TypeDeclarationNode ");
		if (ident != null)
			ident.print();

		if (type != null)
			type.print();

		unindent();
	}
	
	
	public AbstractDescr compile(SymbolTable sym) {
		AbstractDescr d = null;
		
		String identName = ((MyIdNode)ident).strval;
		
		if(type instanceof MyIdNode){
			d = sym.getDescr(identName);
		}else if(type instanceof MyArrayTypeNode || type instanceof MyRecordTypeNode){
			d = type.compile(sym);
		}
		
		sym.declare(identName, d);
		return d;
//
//		if(type instanceof MyIdNode){
//			d = new SimpleTypeDescr(((MyIdNode) type).strval);
//		}else{
//			d = type.compile(sym);
//		}
//		identList.compile(sym, d);
//		
//		
//		IntConstDescr value = new IntConstDescr(); 
//		value.setSize(1);
////		if(expression instanceof MyBinNode){
////			value = value.setIntVal(((MyBinNode)expression).g);
////		}
//		if(type instanceof MyIntNode){
//			value.setIntVal(((MyIntNode)type).getIntVal());
//			
//		}
//		sym.declare(ident.toString(), value);
//		write("GETSP");
//		write("PUSHI, "+value.getSize());
//		write("ADD");
//		write("SETSP");
//		write("PUSHI, "+value.getIntVal());
//		write("PUSHI, "+sym.getAdr(ident.toString()));
//		write("ASSIGN, 1");
//		return value;
	}

}
